package com.hangwei.paperless.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.ResultBean;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张龙臣 on 2020/11/18.
 */

public class ResultAdapter extends RecyclerView.Adapter {

    private final boolean mAble;
    private Context mContext;

    public List<ResultBean> getResultItems() {
        return mResultItems;
    }

    private List<ResultBean> mResultItems;

    public ResultAdapter(ArrayList<ResultBean> resultItems, boolean able) {
        mResultItems = resultItems;
        mAble = able;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext = viewGroup.getContext();
        return new RecyclerView.ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_result, viewGroup, false)) {
        };
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        CheckBox checkBox = viewHolder.itemView.findViewById(R.id.checkbox);
        final TextView tvType = viewHolder.itemView.findViewById(R.id.tvType);
        final ResultBean bean = mResultItems.get(i);
        checkBox.setEnabled(mAble);

        if (bean.isChecked){
            checkBox.setChecked(true);
            tvType.setTextColor(mContext.getResources().getColor(R.color.blue1));
        }else {
            checkBox.setChecked(false);
            tvType.setTextColor(mContext.getResources().getColor(R.color.black));
        }
        tvType.setText(Tools.checkedString(bean.name));
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    for (ResultBean b : mResultItems) {
                        b.isChecked = false;
                    }
                    bean.isChecked = true;
                    notifyDataSetChanged();
                }else {
                    bean.isChecked = false;
                    tvType.setTextColor(mContext.getResources().getColor(R.color.black));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mResultItems == null ? 0 : mResultItems.size();
    }
}
